﻿using Mirror;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class UtilsGame
{
    public delegate void CallbackStock(Stock num);
    public static bool IsOfShips(Ship ship, GameObject obj)
    {
        if(obj == null || ship == null)
        {
            return false;
        }
        var findShip = FindShip(obj);
        return ship != findShip;
    }

    public static bool IsServer(GameObject obj)
    {
        return (OnlineManager.singleton && 
            OnlineManager.singleton.isNetworkActive &&
            obj && 
            obj.GetComponent<NetworkBehaviour>() &&
            obj.GetComponent<NetworkBehaviour>().isServer);

    }

    public static bool IsOnline()
    {
        if(OnlineManager.singleton != null &&
            OnlineManager.singleton.gameObject.activeSelf && 
            OnlineManager.singleton.isNetworkActive)
        {
            return true;
        }
        return false;
    }
    public static void SpawnInServer(GameObject obj)
    {
        if(obj && (OnlineManager.singleton && OnlineManager.singleton.isNetworkActive))
        {
            NetworkServer.Spawn(obj);
        }
    }
    public static Ship FindShip(GameObject obj)
    {
        while (obj)
        {
            Ship foundShip = obj.GetComponent<Ship>();
            if (foundShip )
            {
                return foundShip;
            }
            UAVFunction foundUVA = obj.GetComponent<UAVFunction>();
            if (foundUVA && foundUVA.Ship && foundUVA.Ship.GetComponent<Ship>())
                return foundUVA.Ship.GetComponent<Ship>();

            if (obj.transform.parent)
                obj = obj.transform.parent.gameObject;
            else
                obj = null;
        }
        return null;
    }

    public static bool IsSteamMG()
    {

        var steamMG = GameObject.FindGameObjectWithTag("SteamMG");
        if (steamMG && steamMG.activeSelf && SteamManager.Initialized)
        {
            return true;
        }
        return false;
    }
    public static bool CheckSelectShip()
    {
        if (Player.Instance.m_DriveLevel == Driver.DriveLevel.LEVEL_NULL ||
            Player.Instance.GetCurrentShip() == null)
        {
            MessageShow.Instance.Messaage("你没有驾驶船，你可以去船坞看看。");
            return false;
        }
        return true;
    }

    public static int GetSeedToday()
    {
        return DateTime.Now.Year * 360 + DateTime.Now.Month * 30 + DateTime.Now.Day;
    }
    public static int GetSeedNow()
    {
        return (DateTime.Now.Hour * 60 + DateTime.Now.Minute) * 60 + DateTime.Now.Second;
    }

    public static float GetPrice(Item item)
    {
        float value = ((UtilsMath.rand((int)item.m_Type, GetSeedToday()) + 1));
        value = Mathf.Clamp(value, 0.3f, 1.7f);

        return item.m_Price * 0.5f * value;
    }
}